package com.liqiye.paymanager.controller;

import cn.hutool.core.util.ReUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.liqiye.paymanager.service.payment.AirwallexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * Airwallex 云汇支付
 */
@Slf4j
@RestController
@RequestMapping("/airwallex")
public class AirwallexController {


    @Autowired
    private AirwallexService airwallexService;

    @PostMapping("/customer/create")
    public String createCustomer() {
        airwallexService.createCustomer();
        return "success";
    }

    @PostMapping("/intent/create")
    public Map<String, Object> createIntent(@RequestParam String amount, @RequestParam String applePayToken) {
        return airwallexService.createPaymentIntent(UUID.randomUUID().toString(), amount, "USD", "order_" + System.currentTimeMillis(), applePayToken);
    }

    @PostMapping("/intent/confirm")
    public Map<String, Object> confirmIntent(@RequestParam String id) {
        return airwallexService.confirmPaymentIntent(id);
    }

    @PostMapping("/intent/renew")
    public Map<String, Object> renew(@RequestParam String paymentIntentId, @RequestParam String customerId) {
        return airwallexService.renew(paymentIntentId, null, customerId);
    }

    @PostMapping("/intent/refund")
    public Map<String, Object> refund() {
        return airwallexService.refund();
    }

    /**
     * Webhook 回调
     */
    @PostMapping("/webhook")
    public String webhook(@RequestBody Map<String, Object> payload) {
        log.info("Webhook received: {}", payload);
        // 根据 event_type 处理订单状态
        return "OK";
    }


}
